package cn.tedu.medicalappointmentsystemk.controller;

import cn.tedu.medicalappointmentsystemk.pojo.bo.ScheduleSelectAllBO;
import cn.tedu.medicalappointmentsystemk.pojo.bo.ScheduleSelectBO;
import cn.tedu.medicalappointmentsystemk.pojo.param.ScheduleAddParam;
import cn.tedu.medicalappointmentsystemk.pojo.po.PageData;
import cn.tedu.medicalappointmentsystemk.response.JsonResult;
import cn.tedu.medicalappointmentsystemk.service.ScheduleService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/schedules")
@Slf4j
@Api(tags = "排班模块")
public class ScheduleController {
    @Autowired
    private ScheduleService scheduleService;
    @PostMapping("add")
    @PreAuthorize("hasAuthority('/schedules/insert')")
    @ApiOperationSupport(order = 710)
    @ApiOperation("新增排班")
    public JsonResult scheduleAdd(ScheduleAddParam param){
        log.info("开始执行新增排班功能,入参:{}",param);
        scheduleService.insert(param);
        log.info("执行新增排班功能结束");
        return JsonResult.ok();
    }

    @PostMapping("select/pageNum={pageNum}")
    @PreAuthorize("hasAuthority('/schedules/select')")
    @ApiOperationSupport(order = 720)
    @ApiOperation("根据当前日期查询排班列表")
    public JsonResult scheduleSelect(@PathVariable Integer pageNum,@RequestBody String[] dates){
        log.info("开始执行根据当前日期查询排班功能,入参:{}",(Object) dates);
        PageData<ScheduleSelectBO> pageData = scheduleService.selectByString(pageNum,dates);
        log.info("执行根据当前日期查询排班功能结束,出参:{}",pageData);
        return JsonResult.ok(pageData);
    }

    @GetMapping("selectAll/pageNum={pageNum}")
    @PreAuthorize("hasAnyAuthority('/schedules/select')")
    @ApiOperationSupport(order = 730)
    @ApiOperation("全部排班查询")
    public JsonResult scheduleSelectAll(@PathVariable Integer pageNum){
        log.info("开始执行全部排班查询");
        PageData<ScheduleSelectAllBO> pageData = scheduleService.selectAll(pageNum);
        log.info("执行全部排班查询完成,出参:{}",pageData);
        return JsonResult.ok(pageData);
    }

    @PostMapping("{id}/delete")
    @PreAuthorize("hasAnyAuthority('/schedules/delete')")
    @ApiOperationSupport(order = 740)
    @ApiOperation("根据Id删除排班")
    public JsonResult scheduleDelete(@PathVariable Long id){
        log.info("排班id:{}",id);
        scheduleService.deleteById(id);
        return JsonResult.ok();
    }

    @GetMapping("searchLastMonth/{pageNum}")
    @PreAuthorize("hasAuthority('/patients/select')")
    @ApiOperationSupport(order = 450)
    @ApiOperation("查询最近30天内注册的患者")
    public JsonResult searchLastMonth(@PathVariable Integer pageNum){
        PageData<ScheduleSelectAllBO> pageData = scheduleService.searchLastMonth(pageNum);
        return JsonResult.ok(pageData);
    }

    @GetMapping("searchLastWeek/{pageNum}")
    @PreAuthorize("hasAuthority('/patients/select')")
    @ApiOperationSupport(order = 450)
    @ApiOperation("查询最近7天内注册的患者")
    public JsonResult searchLastWeek(@PathVariable Integer pageNum){
        PageData<ScheduleSelectAllBO> pageData = scheduleService.searchLastWeek(pageNum);
        return JsonResult.ok(pageData);
    }

    @GetMapping("searchToday/{pageNum}")
    @PreAuthorize("hasAuthority('/patients/select')")
    @ApiOperationSupport(order = 450)
    @ApiOperation("查询今天内注册的患者")
    public JsonResult searchToday(@PathVariable Integer pageNum){
        PageData<ScheduleSelectAllBO> pageData = scheduleService.searchToday(pageNum);
        return JsonResult.ok(pageData);
    }

    @GetMapping("selectByNickname/{nickname}")
    @PreAuthorize("hasAuthority('/schedules/select')")
    @ApiOperationSupport(order = 720)
    @ApiOperation("根据当前日期查询排班列表")
    public JsonResult scheduleSelect(@PathVariable String nickname){
        log.info("开始执行根据当前日期查询排班功能,入参:{}",nickname);
        List<ScheduleSelectAllBO> pageData = scheduleService.selectByNickname(nickname);
        log.info("执行根据当前日期查询排班功能结束,出参:{}",pageData);
        return JsonResult.ok(pageData);
    }
}
